package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendPunchDailySummaryDAO;
import org.jsola.hr.dto.AttendPunchDailySummaryAddDTO;
import org.jsola.hr.dto.AttendPunchDailySummaryUpdateDTO;
import org.jsola.hr.entity.AttendPunchDailySummaryDO;
import org.jsola.hr.entity.DepInfoDO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.AttendPunchDailySummaryQuery;
import org.jsola.hr.query.DepInfoQuery;
import org.jsola.hr.service.IAttendPunchDailySummaryService;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IDepInfoService;
import org.jsola.hr.vo.AttendPunchDailySummaryListVO;
import org.jsola.hr.vo.AttendPunchDailySummaryVO;
import org.jsola.hr.vo.CompanyVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendPunchDailySummaryServiceImpl")
public class AttendPunchDailySummaryServiceImpl implements IAttendPunchDailySummaryService {

    @Autowired
    private IAttendPunchDailySummaryDAO attendPunchDailySummaryDAO;
    @Autowired
    private IDepInfoService depInfoService;
    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendPunchDailySummaryVO save(AttendPunchDailySummaryAddDTO attendPunchDailySummaryAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendPunchDailySummaryDO attendPunchDailySummaryDO = attendPunchDailySummaryAddDTO.to(AttendPunchDailySummaryDO.class);
        // 保存
        attendPunchDailySummaryDO = save(attendPunchDailySummaryDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendPunchDailySummaryDO.to(AttendPunchDailySummaryVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendPunchDailySummaryUpdateDTO attendPunchDailySummaryUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendPunchDailySummaryDO attendPunchDailySummaryDO = attendPunchDailySummaryUpdateDTO.to(AttendPunchDailySummaryDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendPunchDailySummaryDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendPunchDailySummaryIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendPunchDailySummaryIds);
    }


    @Override
    public AttendPunchDailySummaryVO selectById(String attendPunchDailySummaryId, String siteId) {
        AttendPunchDailySummaryDO attendPunchDailySummaryDO = selectDOById(attendPunchDailySummaryId, siteId);
        if (attendPunchDailySummaryDO == null) {
            return null;
        }
        return attendPunchDailySummaryDO.to(AttendPunchDailySummaryVO.class);
    }

    @Override
    public List<AttendPunchDailySummaryListVO> select(AttendPunchDailySummaryQuery attendPunchDailySummaryQuery, String siteId) {
        List<AttendPunchDailySummaryDO> attendPunchDailySummaryDOList = selectDO(attendPunchDailySummaryQuery, siteId);
        if(CollectionUtils.isEmpty(attendPunchDailySummaryDOList)) {
            return attendPunchDailySummaryDOList == null ? null : new ArrayList<>();
        }
        return attendPunchDailySummaryDOList.stream()
                .map(attendPunchDailySummaryDO -> attendPunchDailySummaryDO.to(AttendPunchDailySummaryListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendPunchDailySummaryQuery attendPunchDailySummaryQuery, String siteId) {
        Example example = buildExample(attendPunchDailySummaryQuery,siteId);
        return attendPunchDailySummaryDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendPunchDailySummaryListVO> selectPage(AttendPunchDailySummaryQuery attendPunchDailySummaryQuery, String siteId) {

        Example example = buildExample(attendPunchDailySummaryQuery,siteId);
        Page<AttendPunchDailySummaryDO> page = attendPunchDailySummaryDAO.selectPageByExample(example,
                attendPunchDailySummaryQuery.getPageNo(),
                attendPunchDailySummaryQuery.getPageSize());

        return page.to(AttendPunchDailySummaryListVO.class);
    }

    @Override
    public AttendPunchDailySummaryDO selectDOById(String attendPunchDailySummaryId, String siteId) {
        return listById(attendPunchDailySummaryId, siteId);
    }

    @Override
    public List<AttendPunchDailySummaryDO> selectDO(AttendPunchDailySummaryQuery attendPunchDailySummaryQuery, String siteId) {
        Example example = buildExample(attendPunchDailySummaryQuery,siteId);
        return attendPunchDailySummaryDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendPunchDailySummaryQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendPunchDailySummaryQuery attendPunchDailySummaryQuery, String siteId) {
        Example example = new Example(AttendPunchDailySummaryDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendPunchDailySummaryQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendPunchDailySummaryQuery.getCompanyId() + "%");
        }
        if (attendPunchDailySummaryQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendPunchDailySummaryQuery.getCompanyName() + "%");
        }
        if (attendPunchDailySummaryQuery.getAttendPlanId() != null) {
            example.and().andEqualTo("attendPlanId", attendPunchDailySummaryQuery.getAttendPlanId());
        }
        if (attendPunchDailySummaryQuery.getAttendPlanName() != null) {
            example.and().andEqualTo("attendPlanName", attendPunchDailySummaryQuery.getAttendPlanName());
        }
        if (attendPunchDailySummaryQuery.getYearly() != null) {
            example.and().andEqualTo("yearly", attendPunchDailySummaryQuery.getYearly());
        }
        if (attendPunchDailySummaryQuery.getMonthly() != null) {
            example.and().andEqualTo("monthly", attendPunchDailySummaryQuery.getMonthly());
        }
        if (attendPunchDailySummaryQuery.getDaily() != null) {
            example.and().andEqualTo("daily", attendPunchDailySummaryQuery.getDaily());
        }
        if (attendPunchDailySummaryQuery.getSummaryDateStart() != null &&
                attendPunchDailySummaryQuery.getSummaryDateEnd() != null) {
            example.and().andBetween("summaryDate", attendPunchDailySummaryQuery.getSummaryDateStart(),
                    attendPunchDailySummaryQuery.getSummaryDateEnd());
        }
        if (attendPunchDailySummaryQuery.getSummaryDateStart() != null &&
                attendPunchDailySummaryQuery.getSummaryDateEnd() == null) {

            example.and().andGreaterThanOrEqualTo("summaryDate", attendPunchDailySummaryQuery.getSummaryDateStart());
        }
        if (attendPunchDailySummaryQuery.getSummaryDateStart() == null &&
                attendPunchDailySummaryQuery.getSummaryDateEnd() != null) {

            example.and().andLessThanOrEqualTo("summaryDate", attendPunchDailySummaryQuery.getSummaryDateEnd());
        }
        if (attendPunchDailySummaryQuery.getShouldPeoples() != null) {
            example.and().andEqualTo("shouldPeoples", attendPunchDailySummaryQuery.getShouldPeoples());
        }
        if (attendPunchDailySummaryQuery.getActualPeoples() != null) {
            example.and().andEqualTo("actualPeoples", attendPunchDailySummaryQuery.getActualPeoples());
        }
        if (attendPunchDailySummaryQuery.getLatePeoples() != null) {
            example.and().andEqualTo("latePeoples", attendPunchDailySummaryQuery.getLatePeoples());
        }
        if (attendPunchDailySummaryQuery.getEarlyPeoples() != null) {
            example.and().andEqualTo("earlyPeoples", attendPunchDailySummaryQuery.getEarlyPeoples());
        }
        if (attendPunchDailySummaryQuery.getOnWorkMissPeoples() != null) {
            example.and().andEqualTo("onWorkMissPeoples", attendPunchDailySummaryQuery.getOnWorkMissPeoples());
        }
        if (attendPunchDailySummaryQuery.getOffWorkMissPeoples() != null) {
            example.and().andEqualTo("offWorkMissPeoples", attendPunchDailySummaryQuery.getOffWorkMissPeoples());
        }
        if (attendPunchDailySummaryQuery.getLeavePeoples() != null) {
            example.and().andEqualTo("leavePeoples", attendPunchDailySummaryQuery.getLeavePeoples());
        }
        if (attendPunchDailySummaryQuery.getIsUsed() != null) {
            example.and().andEqualTo("isUsed", attendPunchDailySummaryQuery.getIsUsed());
        }
        if (attendPunchDailySummaryQuery.getGmtCreateStart() != null &&
                attendPunchDailySummaryQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendPunchDailySummaryQuery.getGmtCreateStart(),
                    attendPunchDailySummaryQuery.getGmtCreateEnd());
        }
        if (attendPunchDailySummaryQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendPunchDailySummaryQuery.getCreateUserId() + "%");
        }
        if (attendPunchDailySummaryQuery.getGmtModifiedStart() != null &&
                attendPunchDailySummaryQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendPunchDailySummaryQuery.getGmtModifiedStart(),
                    attendPunchDailySummaryQuery.getGmtModifiedEnd());
        }
        if (attendPunchDailySummaryQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendPunchDailySummaryQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendPunchDailySummaryQuery.getOrders());
        return example;
    }
}




